//
//  CYBankView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/7.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYBankView.h"
#import "CYCache.h"
#import "CYDataSourceDelegate.h"
#import "CYBankCell.h"
@interface CYBankView(){
    bankblock _myblock;
    NSURLSessionTask *_task;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CYDataSource *datasource;
@property (nonatomic, strong) CYDelegate *delegate;
@property (nonatomic, strong) CYView *titleBackView;
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYButton *cancelBtn;
@property (nonatomic, strong) NSMutableArray *data;
@end
@implementation CYBankView
- (instancetype)initWithData:(NSMutableArray *)array block:(bankblock)callback{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _myblock = callback;
        self.titleBackView.hidden = self.titleLBL.hidden = self.cancelBtn.hidden = NO;
        [self https];
        
    }
    
    return self;
}
- (CYView *)titleBackView{
    if (!_titleBackView) {
        _titleBackView = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        [self addSubview:_titleBackView];
        
        [_titleBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(90))));
        }];
    }
    return _titleBackView;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"请选择银行" size:AL(PT(30)) textcolor:ColorHex(0x333333)];
        [_titleBackView addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_titleBackView);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[CYButton alloc]initWithImage:@"delete" cornerRadius:0.0];
        [_cancelBtn addTarget:self action:@selector(actionForCancel) forControlEvents:UIControlEventTouchUpInside];
        [_titleBackView addSubview:_cancelBtn];
        
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_titleBackView.mas_right).with.offset(0);
            make.centerY.equalTo(_titleBackView);
            make.size.mas_equalTo(CGSizeMake(AL(PT(90)), AL(PT(90))));
        }];
    }
    return _cancelBtn;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
        [self setupTableview];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleBackView.mas_bottom).with.offset(0);
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.centerX.equalTo(self);
        }];
    }
    
    return _tableView;
}

- (void)setData:(NSMutableArray *)data{
    self.tableView.hidden = NO;
    if (!data)
        _data = [CYCache getCache:@"bankList"];
    if (_data != data && data)
        _data = data;
    _datasource.dataArr = _data;
    [_tableView reloadData];
    [CYCache cache:_data plist:@"bankList"];
}

- (void)setupTableview{
    WS(self)
    _datasource = [[CYDataSource alloc]initWithData:_data identifier:@"CYBankCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    
    _delegate = [[CYDelegate alloc]initWithCellHeight:AL(PT(93)) views:nil block:^(NSIndexPath *index) {
        [weakSelf selectedCell:index];
        [weakSelf hidden];
    }];
    
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    _tableView.dataSource = _datasource;
    _tableView.delegate = _delegate;
    
}

- (void)bindCell:(CYBankCell *)cell dict:(NSDictionary *)dict{
    cell.bank_name = dict[@"bank_name"];
    cell.bank_id = dict[@"id"];
[CYHttps loadingWithFile:dict[@"bank_api_img_url"] complete:^(id data) {
        cell.image = data;
    }];
}

- (void)selectedCell:(NSIndexPath *)index{
    _myblock(_data[index.row][@"id"], _data[index.row][@"bank_name"]);
}

- (void)actionForCancel{
    _myblock(@"",@"");
    [self hidden];
}

- (void)hidden{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
    
}

- (void)https{
    self.data = nil;
  _task = [CYHttps POST:API_bankList parameters:nil showHUD:NO progress:nil success:^(id data) {
        self.data = data;
    } failure:^(id string) {
        self.data = nil;
    }];
    
}

- (void)dealloc{
    [_task cancel];
    _task = nil;

}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
